package com.ITArmy.controller.admin;

import com.ITArmy.beans.Blog;
import com.ITArmy.beans.Shows.SystemMessage;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.service.BlogService;
import com.ITArmy.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Api(value = "后台博客接口层次", tags = "后台博客接口层次Controller层")
@Controller
@RequestMapping("/admin/Blog")
public class AdminBlogController extends BaseController {

    @RequestMapping("/addBlogByBack")
    @ResponseBody
    @ApiOperation(value = "后台添加博客信息，",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse addBlogByBack(Blog blog) {
        blog.setbTime(new Date());
        return blogService.addBlogByBack(blog);
    }

    @RequestMapping("/add")
    @ApiOperation(value = "后台添加博客信息输入界面",
            httpMethod = "POST", response = ServerResponse.class)
    public String add() {
        return "add";
    }

    @RequestMapping("/list")
    @ApiOperation(value = "遍历博客信息",
            httpMethod = "POST", response = ServerResponse.class)
    public String list(@RequestParam(value = "page", defaultValue = "1") int page, Model model) {
        PageHelper.startPage(page,9);
        List<Blog> blogs = blogService.findAllBlog();
        PageInfo blogPageInfo = new PageInfo<>(blogs,9);
        model.addAttribute("blogList",blogPageInfo);
        System.out.println(blogPageInfo);
        return "blog";
    }

/*
    @RequestMapping("/edit")
    @ApiOperation(value = "修改信息时显示原始信息",
            httpMethod = "POST", response = ServerResponse.class)
    public String edit(Long id, Model model) {
        ServerResponse blogServerResponse=blogService.findBlogById(id);
        model.addAttribute("blog",blogServerResponse.getData());
        return "edit";
    }
*/

    @RequestMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改博客信息，", notes = "管理员可修改下面除了ID的所有的参数，用户只能修改标题，内容，类型",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse updateBlog(Blog blog) {
        return blogService.updateBlog(blog);
    }

    @RequestMapping("/findBlogByTitle")
    @ApiOperation(value = "根据标题模糊查询，", httpMethod = "POST", response = ServerResponse.class)
    public String findBlogByTitle(String title, Model model) {
        ServerResponse blogServerResponse = blogService.findBlogByTitle(title);
        model.addAttribute("blogListByTitle", blogServerResponse.getData());
        return "blog";
    }

    @RequestMapping("/findBlogByType")
    @ApiOperation(value = "根据博客类型搜索，", notes = "类型用固定选择",
            httpMethod = "POST", response = ServerResponse.class)
    public String findBlogByType(Integer tid, Model model) {
        ServerResponse blogServerResponse = blogService.findByType(tid);
        model.addAttribute("blogListByType", blogServerResponse);
        return "blog";
    }

    @RequestMapping("/recommend")
    @ResponseBody
    @ApiOperation(value = "设置为推荐，",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse recommend(Long id) {
        return blogService.recommend(id);
    }

    @RequestMapping("/unrecommend")
    @ResponseBody
    @ApiOperation(value = "取消推荐，", httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse unrecommend(Long id) {
        return blogService.unrecommend(id);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除博客信息，", notes = "单个删除",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse delete(Long id) {
        System.out.println("删除的博客的id为：" + id);
        return blogService.deleteBlogById(id);
    }

    @RequestMapping("/deleteMany")
    @ResponseBody
    @ApiOperation(value = "批量删除博客信息，",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse deleteMany(String ids) {
        String[] id = ids.split(",");
        System.out.println("删除的博客的id为：" + ids);
        return blogService.deleteManyBlogByIds(id);
    }
}
